{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "a38e5d2d-7587-4192-90f2-b58e6c62f08c",
   "metadata": {},
   "source": [
    "# Self Discover\n",
    "\n",
    "An implementation of the [Self-Discover paper](https://arxiv.org/pdf/2402.03620.pdf).\n",
    "\n",
    "Based on [this implementation from @catid](https://github.com/catid/self-discover/tree/main?tab=readme-ov-file)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "a18d8f24-5d9a-45c5-9739-6f3c4ed6c9c9",
   "metadata": {},
   "outputs": [],
   "source": [
    "from langchain_openai import ChatOpenAI"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "9f554045-6e79-42d3-be4b-835bbbd0b78c",
   "metadata": {},
   "outputs": [],
   "source": [
    "model = ChatOpenAI(temperature=0, model=\"gpt-4-turbo-preview\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "9e9925aa-638a-4862-823e-9803402b8f82",
   "metadata": {},
   "outputs": [],
   "source": [
    "from langchain import hub\n",
    "from langchain_core.prompts import PromptTemplate"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "c4cc5c8c-f6a5-42c7-9ed5-780d79b3b29a",
   "metadata": {},
   "outputs": [],
   "source": [
    "select_prompt = hub.pull(\"hwchase17/self-discovery-select\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "a5b53d29-f5b6-4f39-af97-bb6b133e1d18",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Select several reasoning modules that are crucial to utilize in order to solve the given task:\n",
      "\n",
      "All reasoning module descriptions:\n",
      "\u001b[33;1m\u001b[1;3m{reasoning_modules}\u001b[0m\n",
      "\n",
      "Task: \u001b[33;1m\u001b[1;3m{task_description}\u001b[0m\n",
      "\n",
      "Select several modules are crucial for solving the task above:\n",
      "\n"
     ]
    }
   ],
   "source": [
    "select_prompt.pretty_print()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "id": "26eaa6bc-5202-4b22-9522-33f227c8eb55",
   "metadata": {},
   "outputs": [],
   "source": [
    "adapt_prompt = hub.pull(\"hwchase17/self-discovery-adapt\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "id": "dc30afb9-180d-417b-9935-f7ef166710b8",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Rephrase and specify each reasoning module so that it better helps solving the task:\n",
      "\n",
      "SELECTED module descriptions:\n",
      "\u001b[33;1m\u001b[1;3m{selected_modules}\u001b[0m\n",
      "\n",
      "Task: \u001b[33;1m\u001b[1;3m{task_description}\u001b[0m\n",
      "\n",
      "Adapt each reasoning module description to better solve the task:\n",
      "\n"
     ]
    }
   ],
   "source": [
    "adapt_prompt.pretty_print()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "a93253a9-8f50-49dd-8815-c3927bae1905",
   "metadata": {},
   "outputs": [],
   "source": [
    "structured_prompt = hub.pull(\"hwchase17/self-discovery-structure\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "id": "8ea8dd78-4285-400b-83d2-c4a241903a79",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Operationalize the reasoning modules into a step-by-step reasoning plan in JSON format:\n",
      "\n",
      "Here's an example:\n",
      "\n",
      "Example task:\n",
      "\n",
      "If you follow these instructions, do you return to the starting point? Always face forward. Take 1 step backward. Take 9 steps left. Take 2 steps backward. Take 6 steps forward. Take 4 steps forward. Take 4 steps backward. Take 3 steps right.\n",
      "\n",
      "Example reasoning structure:\n",
      "\n",
      "{\n",
      "    \"Position after instruction 1\":\n",
      "    \"Position after instruction 2\":\n",
      "    \"Position after instruction n\":\n",
      "    \"Is final position the same as starting position\":\n",
      "}\n",
      "\n",
      "Adapted module description:\n",
      "\u001b[33;1m\u001b[1;3m{adapted_modules}\u001b[0m\n",
      "\n",
      "Task: \u001b[33;1m\u001b[1;3m{task_description}\u001b[0m\n",
      "\n",
      "Implement a reasoning structure for solvers to follow step-by-step and arrive at correct answer.\n",
      "\n",
      "Note: do NOT actually arrive at a conclusion in this pass. Your job is to generate a PLAN so that in the future you can fill it out and arrive at the correct conclusion for tasks like this\n"
     ]
    }
   ],
   "source": [
    "structured_prompt.pretty_print()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "id": "f3d4d79d-f414-4588-b476-4a35b3ba6fbf",
   "metadata": {},
   "outputs": [],
   "source": [
    "reasoning_prompt = hub.pull(\"hwchase17/self-discovery-reasoning\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "id": "23d1e32e-d12e-454a-8484-c08e250e3262",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Follow the step-by-step reasoning plan in JSON to correctly solve the task. Fill in the values following the keys by reasoning specifically about the task given. Do not simply rephrase the keys.\n",
      "    \n",
      "Reasoning Structure:\n",
      "\u001b[33;1m\u001b[1;3m{reasoning_structure}\u001b[0m\n",
      "\n",
      "Task: \u001b[33;1m\u001b[1;3m{task_description}\u001b[0m\n"
     ]
    }
   ],
   "source": [
    "reasoning_prompt.pretty_print()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "id": "7b9af01d-da28-4785-b069-efea61905cfa",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "PromptTemplate(input_variables=['reasoning_structure', 'task_instance'], template='Follow the step-by-step reasoning plan in JSON to correctly solve the task. Fill in the values following the keys by reasoning specifically about the task given. Do not simply rephrase the keys.\\n    \\nReasoning Structure:\\n{reasoning_structure}\\n\\nTask: {task_instance}')"
      ]
     },
     "execution_count": 42,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "reasoning_prompt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "id": "399bf160-e257-429f-b27e-66d4063f195f",
   "metadata": {},
   "outputs": [],
   "source": [
    "_prompt = \"\"\"Follow the step-by-step reasoning plan in JSON to correctly solve the task. Fill in the values following the keys by reasoning specifically about the task given. Do not simply rephrase the keys.\\n    \\nReasoning Structure:\\n{reasoning_structure}\\n\\nTask: {task_description}\"\"\"\n",
    "_prompt = PromptTemplate.from_template(_prompt)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "id": "6e047e3a-b1c2-4a3b-abc7-eb84de6874e4",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'https://smith.langchain.com/hub/hwchase17/self-discovery-reasoning/48340707'"
      ]
     },
     "execution_count": 44,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "hub.push(\"hwchase17/self-discovery-reasoning\", _prompt)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 72,
   "id": "7f66d61e-6dcd-4462-b67b-29c5de56e238",
   "metadata": {},
   "outputs": [],
   "source": [
    "class SelfDiscoverState(TypedDict):\n",
    "    reasoning_modules: str\n",
    "    task_description: str\n",
    "    selected_modules: Optional[str]\n",
    "    adapted_modules: Optional[str]\n",
    "    reasoning_structure: Optional[str]\n",
    "    answer: Optional[str]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 73,
   "id": "5c3bd203-7dc1-457e-813f-283aaf059ec0",
   "metadata": {},
   "outputs": [],
   "source": [
    "def select(inputs):\n",
    "    select_chain = select_prompt | model | StrOutputParser()\n",
    "    return {\"selected_modules\": select_chain.invoke(inputs)}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 74,
   "id": "86420da0-7cc2-4659-853e-9c3ef808e47c",
   "metadata": {},
   "outputs": [],
   "source": [
    "def adapt(inputs):\n",
    "    adapt_chain = adapt_prompt | model | StrOutputParser()\n",
    "    return {\"adapted_modules\": adapt_chain.invoke(inputs)}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 75,
   "id": "270a3905-58a3-4650-96ca-e8254040285f",
   "metadata": {},
   "outputs": [],
   "source": [
    "def structure(inputs):\n",
    "    structure_chain = structured_prompt | model | StrOutputParser()\n",
    "    return {\"reasoning_structure\": structure_chain.invoke(inputs)}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 76,
   "id": "55b486cc-36be-497e-9eba-9c8dc228f2d1",
   "metadata": {},
   "outputs": [],
   "source": [
    "def reason(inputs):\n",
    "    reasoning_chain = reasoning_prompt | model | StrOutputParser()\n",
    "    return {\"answer\": reasoning_chain.invoke(inputs)}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 77,
   "id": "d9e2e4ce-dc9d-45a5-a218-34ab4fb62bc4",
   "metadata": {},
   "outputs": [],
   "source": [
    "from langgraph.graph import StateGraph, END\n",
    "from typing import TypedDict, Optional"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 79,
   "id": "26276404-0134-40a0-a796-cc9927c153ee",
   "metadata": {},
   "outputs": [],
   "source": [
    "graph = StateGraph(SelfDiscoverState)\n",
    "graph.add_node(\"select\", select)\n",
    "graph.add_node(\"adapt\", adapt)\n",
    "graph.add_node(\"structure\", structure)\n",
    "graph.add_node(\"reason\", reason)\n",
    "graph.add_edge(\"select\", \"adapt\")\n",
    "graph.add_edge(\"adapt\", \"structure\")\n",
    "graph.add_edge(\"structure\", \"reason\")\n",
    "graph.add_edge(\"reason\", END)\n",
    "graph.set_entry_point(\"select\")\n",
    "app = graph.compile()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 80,
   "id": "29fe385b-cf5d-4581-80e7-55462f5628bb",
   "metadata": {},
   "outputs": [],
   "source": [
    "reasoning_modules = [\n",
    "    \"1. How could I devise an experiment to help solve that problem?\",\n",
    "    \"2. Make a list of ideas for solving this problem, and apply them one by one to the problem to see if any progress can be made.\",\n",
    "    # \"3. How could I measure progress on this problem?\",\n",
    "    \"4. How can I simplify the problem so that it is easier to solve?\",\n",
    "    \"5. What are the key assumptions underlying this problem?\",\n",
    "    \"6. What are the potential risks and drawbacks of each solution?\",\n",
    "    \"7. What are the alternative perspectives or viewpoints on this problem?\",\n",
    "    \"8. What are the long-term implications of this problem and its solutions?\",\n",
    "    \"9. How can I break down this problem into smaller, more manageable parts?\",\n",
    "    \"10. Critical Thinking: This style involves analyzing the problem from different perspectives, questioning assumptions, and evaluating the evidence or information available. It focuses on logical reasoning, evidence-based decision-making, and identifying potential biases or flaws in thinking.\",\n",
    "    \"11. Try creative thinking, generate innovative and out-of-the-box ideas to solve the problem. Explore unconventional solutions, thinking beyond traditional boundaries, and encouraging imagination and originality.\",\n",
    "    # \"12. Seek input and collaboration from others to solve the problem. Emphasize teamwork, open communication, and leveraging the diverse perspectives and expertise of a group to come up with effective solutions.\",\n",
    "    \"13. Use systems thinking: Consider the problem as part of a larger system and understanding the interconnectedness of various elements. Focuses on identifying the underlying causes, feedback loops, and interdependencies that influence the problem, and developing holistic solutions that address the system as a whole.\",\n",
    "    \"14. Use Risk Analysis: Evaluate potential risks, uncertainties, and tradeoffs associated with different solutions or approaches to a problem. Emphasize assessing the potential consequences and likelihood of success or failure, and making informed decisions based on a balanced analysis of risks and benefits.\",\n",
    "    # \"15. Use Reflective Thinking: Step back from the problem, take the time for introspection and self-reflection. Examine personal biases, assumptions, and mental models that may influence problem-solving, and being open to learning from past experiences to improve future approaches.\",\n",
    "    \"16. What is the core issue or problem that needs to be addressed?\",\n",
    "    \"17. What are the underlying causes or factors contributing to the problem?\",\n",
    "    \"18. Are there any potential solutions or strategies that have been tried before? If yes, what were the outcomes and lessons learned?\",\n",
    "    \"19. What are the potential obstacles or challenges that might arise in solving this problem?\",\n",
    "    \"20. Are there any relevant data or information that can provide insights into the problem? If yes, what data sources are available, and how can they be analyzed?\",\n",
    "    \"21. Are there any stakeholders or individuals who are directly affected by the problem? What are their perspectives and needs?\",\n",
    "    \"22. What resources (financial, human, technological, etc.) are needed to tackle the problem effectively?\",\n",
    "    \"23. How can progress or success in solving the problem be measured or evaluated?\",\n",
    "    \"24. What indicators or metrics can be used?\",\n",
    "    \"25. Is the problem a technical or practical one that requires a specific expertise or skill set? Or is it more of a conceptual or theoretical problem?\",\n",
    "    \"26. Does the problem involve a physical constraint, such as limited resources, infrastructure, or space?\",\n",
    "    \"27. Is the problem related to human behavior, such as a social, cultural, or psychological issue?\",\n",
    "    \"28. Does the problem involve decision-making or planning, where choices need to be made under uncertainty or with competing objectives?\",\n",
    "    \"29. Is the problem an analytical one that requires data analysis, modeling, or optimization techniques?\",\n",
    "    \"30. Is the problem a design challenge that requires creative solutions and innovation?\",\n",
    "    \"31. Does the problem require addressing systemic or structural issues rather than just individual instances?\",\n",
    "    \"32. Is the problem time-sensitive or urgent, requiring immediate attention and action?\",\n",
    "    \"33. What kinds of solution typically are produced for this kind of problem specification?\",\n",
    "    \"34. Given the problem specification and the current best solution, have a guess about other possible solutions.\"\n",
    "    \"35. Let’s imagine the current best solution is totally wrong, what other ways are there to think about the problem specification?\"\n",
    "    \"36. What is the best way to modify this current best solution, given what you know about these kinds of problem specification?\"\n",
    "    \"37. Ignoring the current best solution, create an entirely new solution to the problem.\"\n",
    "    # \"38. Let’s think step by step.\"\n",
    "    \"39. Let’s make a step by step plan and implement it with good notation and explanation.\",\n",
    "]\n",
    "\n",
    "\n",
    "task_example = \"Lisa has 10 apples. She gives 3 apples to her friend and then buys 5 more apples from the store. How many apples does Lisa have now?\"\n",
    "\n",
    "task_example = \"\"\"This SVG path element <path d=\"M 55.57,80.69 L 57.38,65.80 M 57.38,65.80 L 48.90,57.46 M 48.90,57.46 L\n",
    "45.58,47.78 M 45.58,47.78 L 53.25,36.07 L 66.29,48.90 L 78.69,61.09 L 55.57,80.69\"/> draws a:\n",
    "(A) circle (B) heptagon (C) hexagon (D) kite (E) line (F) octagon (G) pentagon(H) rectangle (I) sector (J) triangle\"\"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 81,
   "id": "6cbfbe81-f751-42da-843a-f9003ace663d",
   "metadata": {},
   "outputs": [],
   "source": [
    "reasoning_modules_str = \"\\n\".join(reasoning_modules)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 82,
   "id": "d411c7aa-7017-4d67-88b5-43b5d161c34c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'select': {'selected_modules': \"To solve the task of identifying the shape drawn by the given SVG path element, the following reasoning modules are crucial:\\n\\n1. **Critical Thinking (10)**: This involves analyzing the SVG path commands and coordinates logically to understand the shape they form. It requires questioning assumptions (e.g., not assuming the shape based on a quick glance at the coordinates but rather analyzing the path commands) and evaluating the information given in the SVG path data.\\n\\n2. **Simplification (4)**: Simplifying the problem by breaking down the SVG path commands can make it easier to visualize and understand the shape being drawn. This might involve sketching the path based on the commands and coordinates or using a tool to render the SVG path.\\n\\n3. **Systems Thinking (13)**: Understanding the SVG path as part of a larger system (in this case, the SVG coordinate system and how path commands work) helps in comprehending how the individual commands come together to form a complete shape.\\n\\n4. **Analytical Problem Solving (29)**: This task requires data analysis skills to interpret the SVG path commands and coordinates. Understanding how 'M' (moveto), 'L' (lineto), and other commands work is essential for determining the shape.\\n\\n5. **Creative Thinking (11)**: While not as directly applicable as the other modules, creative thinking can aid in visualizing the shape that the path commands are intended to draw, especially if the shape is complex or if the path commands are not immediately clear.\\n\\n6. **Visualization (30)**: Although not explicitly listed, a module focused on visualization would be highly relevant here. Visualizing the path that the 'M' and 'L' commands create from the given coordinates can directly lead to identifying the shape.\\n\\nGiven the task's nature, modules focused on experimentation, risk analysis, stakeholder perspectives, and long-term implications (e.g., 1, 14, 21, 8) are less relevant. The task is primarily analytical and technical, requiring an understanding of SVG path syntax and geometry rather than broader problem-solving or decision-making strategies.\"}}\n",
      "{'adapt': {'adapted_modules': \"1. **Detailed Path Analysis (10)**: This module focuses on a thorough examination of the SVG path commands and their corresponding coordinates to accurately deduce the shape they outline. It involves a critical approach where assumptions are set aside in favor of a detailed analysis of each command (e.g., 'M' for moveto, 'L' for lineto) and how these commands connect points in the SVG coordinate system to form a specific shape.\\n\\n2. **Path Decomposition (4)**: This involves breaking down the SVG path into more manageable segments or components to facilitate a clearer understanding of the overall shape. Techniques might include manually sketching the path as described by the commands and coordinates or utilizing digital tools to render the SVG path, thereby making the shape more apparent and easier to identify.\\n\\n3. **SVG System Analysis (13)**: Emphasizes the importance of understanding the SVG coordinate system and the functionality of path commands within this framework. This module is about seeing the SVG path not just as a series of commands but as part of the broader system of SVG graphics, where each command plays a specific role in shaping the final image.\\n\\n4. **Command Interpretation and Geometry (29)**: This module requires a deep dive into the syntax and semantics of SVG path commands, coupled with geometric reasoning to interpret the shape formed by these commands. Knowledge of how different commands like 'M' (moveto) and 'L' (lineto) contribute to the construction of geometric shapes is crucial for accurately identifying the shape in question.\\n\\n5. **Imaginative Visualization (11)**: While analytical skills are paramount, this module recognizes the role of creative thinking in visualizing the potential shapes that complex or ambiguous path commands might represent. It encourages thinking beyond the obvious and considering multiple geometric possibilities that fit the given path data.\\n\\n6. **Explicit Visualization (30)**: Directly focuses on the ability to visualize the trajectory formed by executing the SVG path commands, particularly 'M' and 'L'. This module is about using visualization techniques, whether mental or through software tools, to trace the path and see the resulting shape, thereby facilitating its identification.\\n\\nBy refining these modules to more directly address the task of interpreting SVG path elements, the process of identifying the drawn shape becomes more structured and focused on the specific skills and knowledge areas that are most relevant to the task.\"}}\n",
      "{'structure': {'reasoning_structure': '```json\\n{\\n  \"Step 1: Detailed Path Analysis\": {\\n    \"Description\": \"Examine each SVG path command and its coordinates to understand the shape outline.\",\\n    \"Actions\": [\\n      {\\n        \"Command\": \"M\",\\n        \"Coordinate\": \"55.57,80.69\",\\n        \"Purpose\": \"Move to starting point without drawing.\"\\n      },\\n      {\\n        \"Command\": \"L\",\\n        \"Coordinate\": \"57.38,65.80\",\\n        \"Purpose\": \"Draw line to this point.\"\\n      },\\n      {\\n        \"Command\": \"M\",\\n        \"Coordinate\": \"57.38,65.80\",\\n        \"Purpose\": \"Move to this point without drawing.\"\\n      },\\n      {\\n        \"Command\": \"L\",\\n        \"Coordinate\": \"48.90,57.46\",\\n        \"Purpose\": \"Draw line to this point.\"\\n      },\\n      {\\n        \"Command\": \"M\",\\n        \"Coordinate\": \"48.90,57.46\",\\n        \"Purpose\": \"Move to this point without drawing.\"\\n      },\\n      {\\n        \"Command\": \"L\",\\n        \"Coordinate\": \"45.58,47.78\",\\n        \"Purpose\": \"Draw line to this point.\"\\n      },\\n      {\\n        \"Command\": \"M\",\\n        \"Coordinate\": \"45.58,47.78\",\\n        \"Purpose\": \"Move to this point without drawing.\"\\n      },\\n      {\\n        \"Command\": \"L\",\\n        \"Coordinate\": \"53.25,36.07\",\\n        \"Purpose\": \"Draw line to this point.\"\\n      },\\n      {\\n        \"Command\": \"L\",\\n        \"Coordinate\": \"66.29,48.90\",\\n        \"Purpose\": \"Draw line to this point.\"\\n      },\\n      {\\n        \"Command\": \"L\",\\n        \"Coordinate\": \"78.69,61.09\",\\n        \"Purpose\": \"Draw line to this point.\"\\n      },\\n      {\\n        \"Command\": \"L\",\\n        \"Coordinate\": \"55.57,80.69\",\\n        \"Purpose\": \"Draw line to this point to close the shape.\"\\n      }\\n    ]\\n  },\\n  \"Step 2: Path Decomposition\": {\\n    \"Description\": \"Break down the path into segments to simplify analysis.\",\\n    \"Segments\": [\\n      \"Segment 1: Move from (55.57,80.69) to (57.38,65.80)\",\\n      \"Segment 2: Move from (57.38,65.80) to (48.90,57.46)\",\\n      \"Segment 3: Move from (48.90,57.46) to (45.58,47.78)\",\\n      \"Segment 4: Move from (45.58,47.78) to (53.25,36.07)\",\\n      \"Segment 5: Move from (53.25,36.07) to (66.29,48.90)\",\\n      \"Segment 6: Move from (66.29,48.90) to (78.69,61.09)\",\\n      \"Segment 7: Move from (78.69,61.09) to (55.57,80.69)\"\\n    ]\\n  },\\n  \"Step 3: SVG System Analysis\": {\\n    \"Description\": \"Understand the role of each command within the SVG coordinate system.\",\\n    \"Analysis\": [\\n      {\\n        \"Command\": \"M\",\\n        \"Role\": \"Defines starting points for new sub-paths.\"\\n      },\\n      {\\n        \"Command\": \"L\",\\n        \"Role\": \"Creates straight lines between points.\"\\n      }\\n    ]\\n  },\\n  \"Step 4: Command Interpretation and Geometry\": {\\n    \"Description\": \"Interpret the geometric shape formed by the path commands.\",\\n    \"Geometric Principles\": [\\n      \"Identify angles and lines created by \\'L\\' commands.\",\\n      \"Determine the number of sides from the number of \\'L\\' commands.\"\\n    ]\\n  },\\n  \"Step 5: Imaginative Visualization\": {\\n    \"Description\": \"Visualize potential shapes that the path commands might represent.\",\\n    \"Visualization Techniques\": [\\n      \"Sketching the path based on command coordinates.\",\\n      \"Mentally visualizing the path progression.\"\\n    ]\\n  },\\n  \"Step 6: Explicit Visualization\": {\\n    \"Description\": \"Use visualization tools to trace the path and see the resulting shape.\",\\n    \"Tools\": [\\n      \"Digital drawing software\",\\n      \"SVG rendering tools\"\\n    ]\\n  },\\n  \"Conclusion\": {\\n    \"Description\": \"Based on the analysis and visualization, identify the shape.\",\\n    \"Options\": [\\n      \"Circle\",\\n      \"Heptagon\",\\n      \"Hexagon\",\\n      \"Kite\",\\n      \"Line\",\\n      \"Octagon\",\\n      \"Pentagon\",\\n      \"Rectangle\",\\n      \"Sector\",\\n      \"Triangle\"\\n    ],\\n    \"Selected Option\": \"\"\\n  }\\n}\\n```'}}\n",
      "{'reason': {'answer': '```json\\n{\\n  \"Step 1: Detailed Path Analysis\": {\\n    \"Description\": \"Examine each SVG path command and its coordinates to understand the shape outline.\",\\n    \"Actions\": [\\n      {\\n        \"Command\": \"M\",\\n        \"Coordinate\": \"55.57,80.69\",\\n        \"Purpose\": \"Move to starting point without drawing.\"\\n      },\\n      {\\n        \"Command\": \"L\",\\n        \"Coordinate\": \"57.38,65.80\",\\n        \"Purpose\": \"Draw line to this point.\"\\n      },\\n      {\\n        \"Command\": \"M\",\\n        \"Coordinate\": \"57.38,65.80\",\\n        \"Purpose\": \"Move to this point without drawing.\"\\n      },\\n      {\\n        \"Command\": \"L\",\\n        \"Coordinate\": \"48.90,57.46\",\\n        \"Purpose\": \"Draw line to this point.\"\\n      },\\n      {\\n        \"Command\": \"M\",\\n        \"Coordinate\": \"48.90,57.46\",\\n        \"Purpose\": \"Move to this point without drawing.\"\\n      },\\n      {\\n        \"Command\": \"L\",\\n        \"Coordinate\": \"45.58,47.78\",\\n        \"Purpose\": \"Draw line to this point.\"\\n      },\\n      {\\n        \"Command\": \"M\",\\n        \"Coordinate\": \"45.58,47.78\",\\n        \"Purpose\": \"Move to this point without drawing.\"\\n      },\\n      {\\n        \"Command\": \"L\",\\n        \"Coordinate\": \"53.25,36.07\",\\n        \"Purpose\": \"Draw line to this point.\"\\n      },\\n      {\\n        \"Command\": \"L\",\\n        \"Coordinate\": \"66.29,48.90\",\\n        \"Purpose\": \"Draw line to this point.\"\\n      },\\n      {\\n        \"Command\": \"L\",\\n        \"Coordinate\": \"78.69,61.09\",\\n        \"Purpose\": \"Draw line to this point.\"\\n      },\\n      {\\n        \"Command\": \"L\",\\n        \"Coordinate\": \"55.57,80.69\",\\n        \"Purpose\": \"Draw line to this point to close the shape.\"\\n      }\\n    ]\\n  },\\n  \"Step 2: Path Decomposition\": {\\n    \"Description\": \"Break down the path into segments to simplify analysis.\",\\n    \"Segments\": [\\n      \"Segment 1: Move from (55.57,80.69) to (57.38,65.80)\",\\n      \"Segment 2: Move from (57.38,65.80) to (48.90,57.46)\",\\n      \"Segment 3: Move from (48.90,57.46) to (45.58,47.78)\",\\n      \"Segment 4: Move from (45.58,47.78) to (53.25,36.07)\",\\n      \"Segment 5: Move from (53.25,36.07) to (66.29,48.90)\",\\n      \"Segment 6: Move from (66.29,48.90) to (78.69,61.09)\",\\n      \"Segment 7: Move from (78.69,61.09) to (55.57,80.69)\"\\n    ]\\n  },\\n  \"Step 3: SVG System Analysis\": {\\n    \"Description\": \"Understand the role of each command within the SVG coordinate system.\",\\n    \"Analysis\": [\\n      {\\n        \"Command\": \"M\",\\n        \"Role\": \"Defines starting points for new sub-paths.\"\\n      },\\n      {\\n        \"Command\": \"L\",\\n        \"Role\": \"Creates straight lines between points.\"\\n      }\\n    ]\\n  },\\n  \"Step 4: Command Interpretation and Geometry\": {\\n    \"Description\": \"Interpret the geometric shape formed by the path commands.\",\\n    \"Geometric Principles\": [\\n      \"Identify angles and lines created by \\'L\\' commands.\",\\n      \"Determine the number of sides from the number of \\'L\\' commands.\"\\n    ]\\n  },\\n  \"Step 5: Imaginative Visualization\": {\\n    \"Description\": \"Visualize potential shapes that the path commands might represent.\",\\n    \"Visualization Techniques\": [\\n      \"Sketching the path based on command coordinates.\",\\n      \"Mentally visualizing the path progression.\"\\n    ]\\n  },\\n  \"Step 6: Explicit Visualization\": {\\n    \"Description\": \"Use visualization tools to trace the path and see the resulting shape.\",\\n    \"Tools\": [\\n      \"Digital drawing software\",\\n      \"SVG rendering tools\"\\n    ]\\n  },\\n  \"Conclusion\": {\\n    \"Description\": \"Based on the analysis and visualization, identify the shape.\",\\n    \"Options\": [\\n      \"Circle\",\\n      \"Heptagon\",\\n      \"Hexagon\",\\n      \"Kite\",\\n      \"Line\",\\n      \"Octagon\",\\n      \"Pentagon\",\\n      \"Rectangle\",\\n      \"Sector\",\\n      \"Triangle\"\\n    ],\\n    \"Selected Option\": \"Pentagon\"\\n  }\\n}\\n```'}}\n",
      "{'__end__': {'reasoning_modules': '1. How could I devise an experiment to help solve that problem?\\n2. Make a list of ideas for solving this problem, and apply them one by one to the problem to see if any progress can be made.\\n4. How can I simplify the problem so that it is easier to solve?\\n5. What are the key assumptions underlying this problem?\\n6. What are the potential risks and drawbacks of each solution?\\n7. What are the alternative perspectives or viewpoints on this problem?\\n8. What are the long-term implications of this problem and its solutions?\\n9. How can I break down this problem into smaller, more manageable parts?\\n10. Critical Thinking: This style involves analyzing the problem from different perspectives, questioning assumptions, and evaluating the evidence or information available. It focuses on logical reasoning, evidence-based decision-making, and identifying potential biases or flaws in thinking.\\n11. Try creative thinking, generate innovative and out-of-the-box ideas to solve the problem. Explore unconventional solutions, thinking beyond traditional boundaries, and encouraging imagination and originality.\\n13. Use systems thinking: Consider the problem as part of a larger system and understanding the interconnectedness of various elements. Focuses on identifying the underlying causes, feedback loops, and interdependencies that influence the problem, and developing holistic solutions that address the system as a whole.\\n14. Use Risk Analysis: Evaluate potential risks, uncertainties, and tradeoffs associated with different solutions or approaches to a problem. Emphasize assessing the potential consequences and likelihood of success or failure, and making informed decisions based on a balanced analysis of risks and benefits.\\n16. What is the core issue or problem that needs to be addressed?\\n17. What are the underlying causes or factors contributing to the problem?\\n18. Are there any potential solutions or strategies that have been tried before? If yes, what were the outcomes and lessons learned?\\n19. What are the potential obstacles or challenges that might arise in solving this problem?\\n20. Are there any relevant data or information that can provide insights into the problem? If yes, what data sources are available, and how can they be analyzed?\\n21. Are there any stakeholders or individuals who are directly affected by the problem? What are their perspectives and needs?\\n22. What resources (financial, human, technological, etc.) are needed to tackle the problem effectively?\\n23. How can progress or success in solving the problem be measured or evaluated?\\n24. What indicators or metrics can be used?\\n25. Is the problem a technical or practical one that requires a specific expertise or skill set? Or is it more of a conceptual or theoretical problem?\\n26. Does the problem involve a physical constraint, such as limited resources, infrastructure, or space?\\n27. Is the problem related to human behavior, such as a social, cultural, or psychological issue?\\n28. Does the problem involve decision-making or planning, where choices need to be made under uncertainty or with competing objectives?\\n29. Is the problem an analytical one that requires data analysis, modeling, or optimization techniques?\\n30. Is the problem a design challenge that requires creative solutions and innovation?\\n31. Does the problem require addressing systemic or structural issues rather than just individual instances?\\n32. Is the problem time-sensitive or urgent, requiring immediate attention and action?\\n33. What kinds of solution typically are produced for this kind of problem specification?\\n34. Given the problem specification and the current best solution, have a guess about other possible solutions.35. Let’s imagine the current best solution is totally wrong, what other ways are there to think about the problem specification?36. What is the best way to modify this current best solution, given what you know about these kinds of problem specification?37. Ignoring the current best solution, create an entirely new solution to the problem.39. Let’s make a step by step plan and implement it with good notation and explanation.', 'task_description': 'This SVG path element <path d=\"M 55.57,80.69 L 57.38,65.80 M 57.38,65.80 L 48.90,57.46 M 48.90,57.46 L\\n45.58,47.78 M 45.58,47.78 L 53.25,36.07 L 66.29,48.90 L 78.69,61.09 L 55.57,80.69\"/> draws a:\\n(A) circle (B) heptagon (C) hexagon (D) kite (E) line (F) octagon (G) pentagon(H) rectangle (I) sector (J) triangle', 'selected_modules': \"To solve the task of identifying the shape drawn by the given SVG path element, the following reasoning modules are crucial:\\n\\n1. **Critical Thinking (10)**: This involves analyzing the SVG path commands and coordinates logically to understand the shape they form. It requires questioning assumptions (e.g., not assuming the shape based on a quick glance at the coordinates but rather analyzing the path commands) and evaluating the information given in the SVG path data.\\n\\n2. **Simplification (4)**: Simplifying the problem by breaking down the SVG path commands can make it easier to visualize and understand the shape being drawn. This might involve sketching the path based on the commands and coordinates or using a tool to render the SVG path.\\n\\n3. **Systems Thinking (13)**: Understanding the SVG path as part of a larger system (in this case, the SVG coordinate system and how path commands work) helps in comprehending how the individual commands come together to form a complete shape.\\n\\n4. **Analytical Problem Solving (29)**: This task requires data analysis skills to interpret the SVG path commands and coordinates. Understanding how 'M' (moveto), 'L' (lineto), and other commands work is essential for determining the shape.\\n\\n5. **Creative Thinking (11)**: While not as directly applicable as the other modules, creative thinking can aid in visualizing the shape that the path commands are intended to draw, especially if the shape is complex or if the path commands are not immediately clear.\\n\\n6. **Visualization (30)**: Although not explicitly listed, a module focused on visualization would be highly relevant here. Visualizing the path that the 'M' and 'L' commands create from the given coordinates can directly lead to identifying the shape.\\n\\nGiven the task's nature, modules focused on experimentation, risk analysis, stakeholder perspectives, and long-term implications (e.g., 1, 14, 21, 8) are less relevant. The task is primarily analytical and technical, requiring an understanding of SVG path syntax and geometry rather than broader problem-solving or decision-making strategies.\", 'adapted_modules': \"1. **Detailed Path Analysis (10)**: This module focuses on a thorough examination of the SVG path commands and their corresponding coordinates to accurately deduce the shape they outline. It involves a critical approach where assumptions are set aside in favor of a detailed analysis of each command (e.g., 'M' for moveto, 'L' for lineto) and how these commands connect points in the SVG coordinate system to form a specific shape.\\n\\n2. **Path Decomposition (4)**: This involves breaking down the SVG path into more manageable segments or components to facilitate a clearer understanding of the overall shape. Techniques might include manually sketching the path as described by the commands and coordinates or utilizing digital tools to render the SVG path, thereby making the shape more apparent and easier to identify.\\n\\n3. **SVG System Analysis (13)**: Emphasizes the importance of understanding the SVG coordinate system and the functionality of path commands within this framework. This module is about seeing the SVG path not just as a series of commands but as part of the broader system of SVG graphics, where each command plays a specific role in shaping the final image.\\n\\n4. **Command Interpretation and Geometry (29)**: This module requires a deep dive into the syntax and semantics of SVG path commands, coupled with geometric reasoning to interpret the shape formed by these commands. Knowledge of how different commands like 'M' (moveto) and 'L' (lineto) contribute to the construction of geometric shapes is crucial for accurately identifying the shape in question.\\n\\n5. **Imaginative Visualization (11)**: While analytical skills are paramount, this module recognizes the role of creative thinking in visualizing the potential shapes that complex or ambiguous path commands might represent. It encourages thinking beyond the obvious and considering multiple geometric possibilities that fit the given path data.\\n\\n6. **Explicit Visualization (30)**: Directly focuses on the ability to visualize the trajectory formed by executing the SVG path commands, particularly 'M' and 'L'. This module is about using visualization techniques, whether mental or through software tools, to trace the path and see the resulting shape, thereby facilitating its identification.\\n\\nBy refining these modules to more directly address the task of interpreting SVG path elements, the process of identifying the drawn shape becomes more structured and focused on the specific skills and knowledge areas that are most relevant to the task.\", 'reasoning_structure': '```json\\n{\\n  \"Step 1: Detailed Path Analysis\": {\\n    \"Description\": \"Examine each SVG path command and its coordinates to understand the shape outline.\",\\n    \"Actions\": [\\n      {\\n        \"Command\": \"M\",\\n        \"Coordinate\": \"55.57,80.69\",\\n        \"Purpose\": \"Move to starting point without drawing.\"\\n      },\\n      {\\n        \"Command\": \"L\",\\n        \"Coordinate\": \"57.38,65.80\",\\n        \"Purpose\": \"Draw line to this point.\"\\n      },\\n      {\\n        \"Command\": \"M\",\\n        \"Coordinate\": \"57.38,65.80\",\\n        \"Purpose\": \"Move to this point without drawing.\"\\n      },\\n      {\\n        \"Command\": \"L\",\\n        \"Coordinate\": \"48.90,57.46\",\\n        \"Purpose\": \"Draw line to this point.\"\\n      },\\n      {\\n        \"Command\": \"M\",\\n        \"Coordinate\": \"48.90,57.46\",\\n        \"Purpose\": \"Move to this point without drawing.\"\\n      },\\n      {\\n        \"Command\": \"L\",\\n        \"Coordinate\": \"45.58,47.78\",\\n        \"Purpose\": \"Draw line to this point.\"\\n      },\\n      {\\n        \"Command\": \"M\",\\n        \"Coordinate\": \"45.58,47.78\",\\n        \"Purpose\": \"Move to this point without drawing.\"\\n      },\\n      {\\n        \"Command\": \"L\",\\n        \"Coordinate\": \"53.25,36.07\",\\n        \"Purpose\": \"Draw line to this point.\"\\n      },\\n      {\\n        \"Command\": \"L\",\\n        \"Coordinate\": \"66.29,48.90\",\\n        \"Purpose\": \"Draw line to this point.\"\\n      },\\n      {\\n        \"Command\": \"L\",\\n        \"Coordinate\": \"78.69,61.09\",\\n        \"Purpose\": \"Draw line to this point.\"\\n      },\\n      {\\n        \"Command\": \"L\",\\n        \"Coordinate\": \"55.57,80.69\",\\n        \"Purpose\": \"Draw line to this point to close the shape.\"\\n      }\\n    ]\\n  },\\n  \"Step 2: Path Decomposition\": {\\n    \"Description\": \"Break down the path into segments to simplify analysis.\",\\n    \"Segments\": [\\n      \"Segment 1: Move from (55.57,80.69) to (57.38,65.80)\",\\n      \"Segment 2: Move from (57.38,65.80) to (48.90,57.46)\",\\n      \"Segment 3: Move from (48.90,57.46) to (45.58,47.78)\",\\n      \"Segment 4: Move from (45.58,47.78) to (53.25,36.07)\",\\n      \"Segment 5: Move from (53.25,36.07) to (66.29,48.90)\",\\n      \"Segment 6: Move from (66.29,48.90) to (78.69,61.09)\",\\n      \"Segment 7: Move from (78.69,61.09) to (55.57,80.69)\"\\n    ]\\n  },\\n  \"Step 3: SVG System Analysis\": {\\n    \"Description\": \"Understand the role of each command within the SVG coordinate system.\",\\n    \"Analysis\": [\\n      {\\n        \"Command\": \"M\",\\n        \"Role\": \"Defines starting points for new sub-paths.\"\\n      },\\n      {\\n        \"Command\": \"L\",\\n        \"Role\": \"Creates straight lines between points.\"\\n      }\\n    ]\\n  },\\n  \"Step 4: Command Interpretation and Geometry\": {\\n    \"Description\": \"Interpret the geometric shape formed by the path commands.\",\\n    \"Geometric Principles\": [\\n      \"Identify angles and lines created by \\'L\\' commands.\",\\n      \"Determine the number of sides from the number of \\'L\\' commands.\"\\n    ]\\n  },\\n  \"Step 5: Imaginative Visualization\": {\\n    \"Description\": \"Visualize potential shapes that the path commands might represent.\",\\n    \"Visualization Techniques\": [\\n      \"Sketching the path based on command coordinates.\",\\n      \"Mentally visualizing the path progression.\"\\n    ]\\n  },\\n  \"Step 6: Explicit Visualization\": {\\n    \"Description\": \"Use visualization tools to trace the path and see the resulting shape.\",\\n    \"Tools\": [\\n      \"Digital drawing software\",\\n      \"SVG rendering tools\"\\n    ]\\n  },\\n  \"Conclusion\": {\\n    \"Description\": \"Based on the analysis and visualization, identify the shape.\",\\n    \"Options\": [\\n      \"Circle\",\\n      \"Heptagon\",\\n      \"Hexagon\",\\n      \"Kite\",\\n      \"Line\",\\n      \"Octagon\",\\n      \"Pentagon\",\\n      \"Rectangle\",\\n      \"Sector\",\\n      \"Triangle\"\\n    ],\\n    \"Selected Option\": \"\"\\n  }\\n}\\n```', 'answer': '```json\\n{\\n  \"Step 1: Detailed Path Analysis\": {\\n    \"Description\": \"Examine each SVG path command and its coordinates to understand the shape outline.\",\\n    \"Actions\": [\\n      {\\n        \"Command\": \"M\",\\n        \"Coordinate\": \"55.57,80.69\",\\n        \"Purpose\": \"Move to starting point without drawing.\"\\n      },\\n      {\\n        \"Command\": \"L\",\\n        \"Coordinate\": \"57.38,65.80\",\\n        \"Purpose\": \"Draw line to this point.\"\\n      },\\n      {\\n        \"Command\": \"M\",\\n        \"Coordinate\": \"57.38,65.80\",\\n        \"Purpose\": \"Move to this point without drawing.\"\\n      },\\n      {\\n        \"Command\": \"L\",\\n        \"Coordinate\": \"48.90,57.46\",\\n        \"Purpose\": \"Draw line to this point.\"\\n      },\\n      {\\n        \"Command\": \"M\",\\n        \"Coordinate\": \"48.90,57.46\",\\n        \"Purpose\": \"Move to this point without drawing.\"\\n      },\\n      {\\n        \"Command\": \"L\",\\n        \"Coordinate\": \"45.58,47.78\",\\n        \"Purpose\": \"Draw line to this point.\"\\n      },\\n      {\\n        \"Command\": \"M\",\\n        \"Coordinate\": \"45.58,47.78\",\\n        \"Purpose\": \"Move to this point without drawing.\"\\n      },\\n      {\\n        \"Command\": \"L\",\\n        \"Coordinate\": \"53.25,36.07\",\\n        \"Purpose\": \"Draw line to this point.\"\\n      },\\n      {\\n        \"Command\": \"L\",\\n        \"Coordinate\": \"66.29,48.90\",\\n        \"Purpose\": \"Draw line to this point.\"\\n      },\\n      {\\n        \"Command\": \"L\",\\n        \"Coordinate\": \"78.69,61.09\",\\n        \"Purpose\": \"Draw line to this point.\"\\n      },\\n      {\\n        \"Command\": \"L\",\\n        \"Coordinate\": \"55.57,80.69\",\\n        \"Purpose\": \"Draw line to this point to close the shape.\"\\n      }\\n    ]\\n  },\\n  \"Step 2: Path Decomposition\": {\\n    \"Description\": \"Break down the path into segments to simplify analysis.\",\\n    \"Segments\": [\\n      \"Segment 1: Move from (55.57,80.69) to (57.38,65.80)\",\\n      \"Segment 2: Move from (57.38,65.80) to (48.90,57.46)\",\\n      \"Segment 3: Move from (48.90,57.46) to (45.58,47.78)\",\\n      \"Segment 4: Move from (45.58,47.78) to (53.25,36.07)\",\\n      \"Segment 5: Move from (53.25,36.07) to (66.29,48.90)\",\\n      \"Segment 6: Move from (66.29,48.90) to (78.69,61.09)\",\\n      \"Segment 7: Move from (78.69,61.09) to (55.57,80.69)\"\\n    ]\\n  },\\n  \"Step 3: SVG System Analysis\": {\\n    \"Description\": \"Understand the role of each command within the SVG coordinate system.\",\\n    \"Analysis\": [\\n      {\\n        \"Command\": \"M\",\\n        \"Role\": \"Defines starting points for new sub-paths.\"\\n      },\\n      {\\n        \"Command\": \"L\",\\n        \"Role\": \"Creates straight lines between points.\"\\n      }\\n    ]\\n  },\\n  \"Step 4: Command Interpretation and Geometry\": {\\n    \"Description\": \"Interpret the geometric shape formed by the path commands.\",\\n    \"Geometric Principles\": [\\n      \"Identify angles and lines created by \\'L\\' commands.\",\\n      \"Determine the number of sides from the number of \\'L\\' commands.\"\\n    ]\\n  },\\n  \"Step 5: Imaginative Visualization\": {\\n    \"Description\": \"Visualize potential shapes that the path commands might represent.\",\\n    \"Visualization Techniques\": [\\n      \"Sketching the path based on command coordinates.\",\\n      \"Mentally visualizing the path progression.\"\\n    ]\\n  },\\n  \"Step 6: Explicit Visualization\": {\\n    \"Description\": \"Use visualization tools to trace the path and see the resulting shape.\",\\n    \"Tools\": [\\n      \"Digital drawing software\",\\n      \"SVG rendering tools\"\\n    ]\\n  },\\n  \"Conclusion\": {\\n    \"Description\": \"Based on the analysis and visualization, identify the shape.\",\\n    \"Options\": [\\n      \"Circle\",\\n      \"Heptagon\",\\n      \"Hexagon\",\\n      \"Kite\",\\n      \"Line\",\\n      \"Octagon\",\\n      \"Pentagon\",\\n      \"Rectangle\",\\n      \"Sector\",\\n      \"Triangle\"\\n    ],\\n    \"Selected Option\": \"Pentagon\"\\n  }\\n}\\n```'}}\n"
     ]
    }
   ],
   "source": [
    "for s in app.stream(\n",
    "    {\"task_description\": task_example, \"reasoning_modules\": reasoning_modules_str}\n",
    "):\n",
    "    print(s)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ea8568d5-bdb6-45cd-8d04-1ab305786caa",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c14a291c-7c1b-43bc-807e-11180290985e",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
